package com.goodfox.web.generic.function.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goodfox.web.generic.function.entity.Function;
import com.goodfox.web.generic.function.mapper.FunctionMapper;
import com.goodfox.web.generic.function.service.FunctionService;
import com.goodfox.web.generic.provide.annotation.NoteExplain;
import com.goodfox.web.generic.role.entity.Role;
import com.goodfox.web.generic.role.service.impl.RoleServiceImpl;

@Service(value="functionService")
@NoteExplain(comment="功能管理")
public class FunctionServiceImpl implements FunctionService{

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(FunctionServiceImpl.class);

	@Autowired
	private FunctionMapper functionMapper;

	@NoteExplain(comment="查询功能列表")
	public Set<Function> queryFunctionSetByScope(String roleScope){
		return orderFillingFunSet(queryFunctionListByScope(roleScope,"1"));
	}

	@NoteExplain(comment="查询功能列表")
	public List<Function> queryFunctionListByScope(String roleScope,String funState){
		Map map=new HashMap();
		map.put("roleScope", roleScope);
		map.put("funState", funState);
		return functionMapper.selectFunctionList(map);
	}

	@NoteExplain(comment="获取功能")
	public Function getFunction(String funId){
		return functionMapper.selectFunctionById(funId);
	}

	@NoteExplain(comment="新增功能",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addFunction(Function entity,String loginLogId){
		entity.setFunState("1");
		if(entity.getFunGradeParent()!=null && !entity.getFunGradeParent().equals("")){
			entity.setFunGrade(entity.getFunGradeParent()+1);
		}else{
			entity.setFunGrade(1L);
		}
		if(entity.getFunSort()==null){
			Map map=new HashMap();
			if(entity.getFunIdParent()==null || entity.getFunIdParent().equals("")){
				map.put("funIdParent", null);
			}else{
				map.put("funIdParent", entity.getFunIdParent());
			}
			entity.setFunSort(functionMapper.selectSubFunctionListCount(map)+1);
		}
		functionMapper.insertFunction(entity);
	}

	@NoteExplain(comment="更新功能",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyFunction(Function oldEny,Function newEny,String loginLogId){
		if(newEny.getFunGradeParent()!=null && !newEny.getFunGradeParent().equals("")){
			newEny.setFunGrade(newEny.getFunGradeParent()+1);
		}else{
			newEny.setFunGrade(1L);
		}
		if(newEny.getFunSort()==null){
			Map map=new HashMap();
			if(newEny.getFunIdParent()==null || newEny.getFunIdParent().equals("")){
				map.put("funIdParent", null);
			}else{
				map.put("funIdParent", newEny.getFunIdParent());
			}
			newEny.setFunSort(functionMapper.selectSubFunctionListCount(map)+1);
		}
		functionMapper.updateFunction(newEny);
	}
	
	@NoteExplain(comment="移除功能",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeFunction(Function entity,String loginLogId){
		functionMapper.deleteFunctionById(entity.getFunId());
	}

	@NoteExplain(comment="查询功能集合")
	public Set<Function> queryFunctionSetByRoleId(String roleId){
		Set<Function> funSet=new HashSet<Function>();
		Map map=new HashMap();
		map.put("roleId", roleId);
		for(Function function:functionMapper.selectRoleFunctionList(map)){
			funSet.add(function);
		}
		return orderFillingFunSet(funSet);
	}

	@NoteExplain(comment="查询功能集合")
	public Set<Function> queryMenuFunSetByRoleId(String roleId){
		Set<Function> funSet=new HashSet<Function>();
		Map map=new HashMap();
		map.put("roleId", roleId);
		map.put("funUse", "1");
		for(Function function:functionMapper.selectRoleFunctionList(map)){
			funSet.add(function);
		}
		return orderFillingFunSet(funSet);
	}

	@NoteExplain(comment="填充功能集合")
	public Set<Function> orderFillingFunSet(List<Function> funList){
		TreeSet<Function> topFunSet=new TreeSet<Function>();
		Set<Function> childFunSet=new HashSet<Function>();
		for(Function orgFun:funList){
			if(orgFun.getFunGrade().equals(1L)){
				topFunSet.add(orgFun);
			}else{
				childFunSet.add(orgFun);
			}
		}
		for(Function topFun:topFunSet){
			funAddChild(topFun,childFunSet,0);
		}
		return topFunSet;
	}

	@NoteExplain(comment="填充功能集合")
	public Set<Function> orderFillingFunSet(Set<Function> funSet){
		TreeSet<Function> topFunSet=new TreeSet<Function>();
		Set<Function> childFunSet=new HashSet<Function>();
		for(Function orgFun:funSet){
			if(orgFun.getFunGrade().equals(1L)){
				topFunSet.add(orgFun);
			}else{
				childFunSet.add(orgFun);
			}
		}
		for(Function topFun:topFunSet){
			funAddChild(topFun,childFunSet,0);
		}
		return topFunSet;
	}
	
	@NoteExplain(comment="功能添加子功能")
	private void funAddChild(Function topFun,Set<Function> orgFunSet,int count){
		TreeSet<Function> funSet=new TreeSet<Function>();
		for(Function orgFun:orgFunSet){
			if(null!=orgFun.getFunIdParent()){
				if(topFun.getFunId().equals(orgFun.getFunIdParent())){
					funSet.add(orgFun);
					count++;
				}
			}
		}
		if(funSet.size()>0){
			topFun.setRealSubFunSet(funSet);
			for(Function fun:topFun.getRealSubFunSet()){
				funAddChild(fun,orgFunSet,count);
			}
			topFun.setRealSubCount((long)topFun.getRealSubFunSet().size());
		}
		if(count==orgFunSet.size()){
			return;
		}
	}
}
